package com.kanqiubao.kq.fragment;


import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kanqiubao.kq.R;
import com.kanqiubao.kq.adapter.BaseAdapter;
import com.kanqiubao.kq.utils.StringUtil;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import okhttp3.Call;

public abstract class SmartBaseRefreshFragment<T> extends BaseRefreshFragment<T> implements OnRefreshLoadMoreListener, OnItemClickListener {
    protected RecyclerView mRecyclerView;
    protected BaseQuickAdapter<T, BaseViewHolder> mSuperBaseAdapter;
    protected SmartRefreshLayout mSmartRefreshLayout;
    protected boolean isFirst;
    protected int mType;

    public void setmType(int i) {
        mType = i;
    }

    @Override
    protected void initView() {
        mRecyclerView = findView(R.id.recyclerView);
        mSmartRefreshLayout = findView(R.id.refreshLayout);
        if (mSuperBaseAdapter != null && mRecyclerView != null) {
            mRecyclerView.setAdapter(mSuperBaseAdapter);
            mSuperBaseAdapter.setOnItemClickListener(this);

        }
        mSmartRefreshLayout.setOnRefreshLoadMoreListener(this);
        if (!StringUtil.isEmptyString(mSearchKey)) {
            onRefresh(mSmartRefreshLayout);
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isRefresh = false;
        pageIndex++;
        requestData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isRefresh = true;
        pageIndex = 1;
        requestData();
    }

    @Override
    public void onSuccess(List<T> paramT, Call paramCall, okhttp3.Response paramResponse) {
        successData(paramT);
        super.onSuccess(paramT, paramCall, paramResponse);
    }

    @Override
    public void onCacheSuccess(List<T> ts, Call call) {
        successData(ts);
        super.onCacheSuccess(ts, call);
    }

    @Override
    public void onError(Call call, okhttp3.Response response, Exception e) {
        error();
        super.onError(call, response, e);
    }

    protected void successData(List<T> data) {
        isFirst = true;
        if (mRecyclerView == null) {
            return;
        }
        mSmartRefreshLayout.finishLoadMore();
        mSmartRefreshLayout.finishRefresh();
        if (isRefresh) {
            if (data != null && data.size() > 0) {
                isVisableView(0);
            } else {
                isVisableView(1);
            }
        }
        if (data != null) {
            List ls = (List) data;
            if (ls.size() > 0) {
                if (isRefresh) {
                    mSuperBaseAdapter.setNewInstance(ls);
                }
                isRefresh = true;
                if (ls.size() < pageSize) {
                    if (mSuperBaseAdapter.getItemCount() < 10) {
                        mSmartRefreshLayout.setEnableLoadMore(false);
                    }
                } else {
                    mSmartRefreshLayout.setEnableLoadMore(true);
                }
                mSuperBaseAdapter.addData(ls);
            } else {
                if (mSuperBaseAdapter.getItemCount() < 10) {
                    mSmartRefreshLayout.setEnableLoadMore(false);
                }
            }
        }
    }

    protected void error() {
        if (isRefresh && mSuperBaseAdapter.getItemCount() == 0) {
            // 网络异常
            isVisableView(2);
        }
        pageIndex = pageIndex == 1 ? 1 : --pageIndex;
        if (mRecyclerView != null) {
            mSmartRefreshLayout.finishLoadMore();
            mSmartRefreshLayout.finishRefresh();
        }
    }


    @Override
    public void setmSearchKey(String mSearchKey) {
        super.setmSearchKey(mSearchKey);
        isFirstLoad = true;
        isRefresh = true;
        pageIndex = 1;
        if (mSmartRefreshLayout != null) {
            onRefresh(mSmartRefreshLayout);
        }
    }

    @Override
    public void initRequest() {
        emptyLayoutClick();
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

    }

    protected List<String> test() {
        List<String> ls = new ArrayList<>();
        ls.add("");
        ls.add("");
        ls.add("");
        ls.add("");
        ls.add("");
        ls.add("");
        return ls;
    }
}
